﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using StripeOne.Core.XmlConfig;
using StripeOne.Core.S1Cache;
using StripeOne.Core.S1Log;

namespace StripeOne.Web.Core.Configuration
{
    public class StripeOneConfig : XmlS1Config
    {
        public StripeOneConfig() : this("default") { }

        public StripeOneConfig(string section) : base("StripeOne", section) { }

        public static StripeOneConfig Default { get { return new StripeOneConfig("default"); } }

        //TODO: Implementar
        //public static StripeOneConfig Current
        //{
        //    get
        //    {
        //        return new StripeOneConfig("brasil");
        //    }
        //}

        public static StripeOneConfig Current
        {
            get
            {
                return S1Cache.AutoCache<StripeOneConfig>("STRIPEONE_GENERALCONFIG_CURRENT_" + StripeOneInstances.GetCurrentHost(), 0, () => GetCurrentNoCache());
            }
        }

        private static StripeOneConfig GetCurrentNoCache()
        {
            try
            {
                string configSection = StripeOneInstances.Current.CurrentDomain.StripeOneConfigSection;
                StripeOneConfig config = new StripeOneConfig(configSection);
                return XmlS1Config.ValidateConfig(config.ConfigFile, configSection) ? config : Default;
            }
            catch (Exception e)
            {
                S1Log.Default.Exception(e);
                return Default;
            }
        }

        public string BaseUrl { get { return GetString("baseUrl"); } }
        public string TestDefault { get { return GetString("test"); } }
    }
}